# -*- coding=utf-8 -*-

import re
from os import listdir
from os.path import isfile, join

from eyed3 import id3

mp3_file_dir = 'D:/data/s1/'
mp3_file_pattern = r'(.+)\.mp3'


def main():
    onlyfiles = [f for f in listdir(mp3_file_dir)
                 if (re.match(mp3_file_pattern, f)) and (isfile(join(mp3_file_dir, f)))]
    print(onlyfiles)
    for mp3_file in onlyfiles:
        fname = get_name(mp3_file)
        print(fname)
        tag = id3.Tag()
        tag.parse(join(mp3_file_dir, mp3_file))
        print(tag.artist)
        print(tag.title)
        tag.title = fname
        tag.save()
        print('==============')


def get_name(file_name):
    matched = re.match(mp3_file_pattern, file_name)
    if matched:
        return matched.group(1)


if __name__ == '__main__':
    main()
